package com.java.controller;

import java.util.Collection;

import com.java.Entry;
import com.java.model.Student;
import com.java.service.StudentService;
import com.java.service.impl.StudentServiceDbImpl;
import com.java.vo.Result;

/**
 * 交互类
 */
public class StudentController {
	
	private StudentService studentService;
	
	public StudentController() {
		/**
		 * 第一种方法：
		 * 直接声明studentService为StudentServiceMapImpl对象
		 */
		this.studentService = new StudentServiceDbImpl();
		// this.studentService = new StudentServiceMapImpl();
		// this.studentService = new StudentServiceListImpl();
		// this.studentService = new StudentServiceSetImpl();
		
		/**
		 * 第二种方法：
		 * 使用动态代理，为StudentServiceMapImpl对象创建代理类，
		 * 在代理处理类中添加对角色权限的判断
		 * 实现AOP操作
		 */
//		RoleHandler handler = new RoleHandler();
//		this.studentService = (StudentService) Proxy.newProxyInstance(
//				this.getClass().getClassLoader(), 
//				new Class[]{StudentService.class}, 
//				handler
//			);
	}
	
	public void add() {
		System.out.println("=====  开始新增学员  =====");
		System.out.println("请输入学员ID");
		int id = Integer.valueOf(Entry.scanner.nextLine());
		System.out.println("请输入学员姓名");
		String name = Entry.scanner.nextLine();
		System.out.println("请输入学员年龄");
		int age = Integer.valueOf(Entry.scanner.nextLine());
		System.out.println("请输入成绩");
		double score = Double.valueOf(Entry.scanner.nextLine());
		
		Result result = studentService.add(id, name, age, score);
		System.out.println(result.getMsg());
		if (result.isStatus()) {
			System.out.println("再来一发? (y or n)");
			if ("y".equalsIgnoreCase(Entry.scanner.nextLine())) {
				add();
			}
		}
		System.out.println("=====  结束新增学员  =====");
	}
	
	public void edit() {
		System.out.println("=====  开始编辑学员  =====");
		System.out.println("请输入需要编辑的学员ID：");
		int id = checkIdValid();
		System.out.println("请输入修改后的学员姓名：");
		String name = Entry.scanner.nextLine();
		System.out.println("请输入修改后的学员年龄");
		String ageStr = Entry.scanner.nextLine();
		System.out.println("请输入修改后的成绩");
		String scoreStr = Entry.scanner.nextLine();
		
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		int age = 0;
		if (ageStr != null && !"".equals(ageStr)) {
			age = Integer.valueOf(ageStr);
		}
		student.setAge(age);
		double score = 0d;
		if (scoreStr != null && !"".equals(scoreStr)) {
			score = Double.valueOf(scoreStr);
		}
		student.setScore(score);
		
		Result result = studentService.edit(student);
		System.out.println(result.getMsg());
		if (result.isStatus()) {
			System.out.println("再来一发? (y or n)");
			if ("y".equalsIgnoreCase(Entry.scanner.nextLine())) {
				edit();
			}
		}
		
		System.out.println("=====  结束编辑学员  =====");
	}
	
	private int checkIdValid() {
		int id = Integer.valueOf(Entry.scanner.nextLine());
		Student student = studentService.find(id);
		if (student == null) {
			System.out.println("该学员不存在，请重新输入");
			checkIdValid();
		}
		return id;
	}
	
	public void delete() {
		System.out.println("=====  开始删除学员  =====");
		System.out.println("请输入需要删除的学员ID：");
		int id = Integer.valueOf(Entry.scanner.nextLine());
		Result result = studentService.delete(id);
		System.out.println(result.getMsg());
		if (result.isStatus()) {
			System.out.println("再来一发? (y or n)");
			if ("y".equalsIgnoreCase(Entry.scanner.nextLine())) {
				add();
			}
		}
		System.out.println("=====  结束删除学员  =====");
	}
	
	public void find() {
		System.out.println("=====  开始查找学员  =====");
		System.out.println("请输入需要查找的学员ID");
		int id = Integer.valueOf(Entry.scanner.nextLine());
		Student student = studentService.find(id);
		if (student == null) {
			System.out.println("该学员不存在");
		} else {
			System.out.println(student);
		}
		System.out.println("=====  结束查找学员  =====");
	}
	
	public void print() {
		System.out.println("=====  开始打印所有学员  =====");
		Collection<Student> studentList = studentService.listAll();
		if (studentList == null) {
			System.out.println("没找到学员");
		} else {
			for (Student student : studentList) {
				System.out.println(student);
			}
		}
		System.out.println("=====  结束打印所有学员  =====");
	}
	
	public void save() {
		System.out.println("=====  开始存储所有学员信息  =====");
		studentService.save();
		System.out.println("=====  结束存储  =====");
	}
	
	public void load() {
		System.out.println("=====  开始加载学员信息  =====");
		studentService.load();
		System.out.println("=====  加载学员信息成功  =====");
	}

}
